package com.wwz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwz.pojo.Customer;
import com.wwz.pojo.query.CustomerQuery;
import com.wwz.service.ICustomerService;
import com.wwz.util.EcoBootException;
import com.wwz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 客户控制层
 * @date 2022/03/26 51:11:05
 */
@RestController
@RequestMapping("/manage/customer")
@Api(tags = "客户控制层")
public class CustomerController {

    @Resource
    private ICustomerService customerService;

    @ApiOperation("客户列表")
    @GetMapping("/toPage")
    public Result toPage(CustomerQuery query){
        IPage<Customer> customerIPage = customerService.toPage(query);
        return Result.ok(customerIPage);
    }


    @ApiOperation("删除客户")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        customerService.removeById(id);
        return Result.ok("ok");
    }

    @ApiOperation("添加或修改客户信息")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Customer customer){

        if (StrUtil.isAllEmpty(customer.getName()
                ,customer.getEmail()
                ,customer.getPhone())){
            throw new EcoBootException("信息为空");
        }
        customerService.addOrUpdate(customer);
        return Result.ok("ok");
    }
}
